﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Estimate
{
    public partial class formAdminConvertor : FormMDIChildTemplate
    {
        public formAdminConvertor()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 1; i < 21; i++)
            {
                string chapter = "Розділ "+i.ToString();
                var roots = context._temps.Where(x => x.F2.Contains(chapter));
                if (roots.Count() > 0)
                {
                    _temp root = roots.First();
                    WorkCategory workRoot = new WorkCategory();
                    workRoot.WorkCategoryName = root.F2.Replace(chapter, "");
                    context.WorkCategories.InsertOnSubmit(workRoot);
                    context.SubmitChanges();
                    for (int j = 1; j < 100; j++)
                    {
                        var works = context._temps.Where(x => x.F1 == i.ToString() + "\\" + j.ToString());
                        if (works.Count() > 0)
                        {
                            _temp root2 = works.First();
                            WorkCategory workRoot2 = new WorkCategory();
                            
                            workRoot2.WorkCategoryName  = root2.F2;
                            workRoot2.WorkCategory1 = workRoot;
                            context.WorkCategories.InsertOnSubmit(workRoot2);
                            context.SubmitChanges();
                            for (int k = 1; k < 100; k++)
                            {
                                var realWorks = context._temps.Where
                                    (x => x.F1 == i.ToString() + "\\" + j.ToString() + "." + k.ToString());
                                if (realWorks.Count() > 0)
                                {
                                    _temp root3 = realWorks.First();
                                    Work work = new Work();
                                    if (root3.F4 != null)
                                        work.Price = (decimal)root3.F4;
                                    else work.Price = 0;
                                    if (root3.F3 == null) continue;

                                    work.Unit = GetUnit(root3.F3);
                                    work.WorkCategory = workRoot2;
                                    work.WorkCode = root3.F1;
                                    work.WorkName = root3.F2;
                                    context.Works.InsertOnSubmit(work);
                                    context.SubmitChanges();
                                }
                            
                            }
                        }
                    }
                }
                context .SubmitChanges();
            }
        }

        private Unit GetUnit(string p)
        {
            var units = context.Units.Where(x => x.UnitName == p);
            if (units.Count() > 0) return units.First();
            else
            {
                Unit unit = new Unit();
                unit.UnitName = p;
              //  unit.UnitTypeID = 2;
                context.Units.InsertOnSubmit(unit);
                context.SubmitChanges();
                return unit;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var workcategories = context.WorkCategories.Where(x => x.WorkCategoryID > 424 && x.WorkCategoryID < 446);
            foreach (WorkCategory workCategory in workcategories)
            {
                Work work = new Work();
                work.Price = 0;
                work.UnitID = 1;
                work.WorkCategory = workCategory;
                work.WorkCode = "";
                work.WorkName = workCategory.WorkCategoryName;
                context.Works.InsertOnSubmit(work);
                context.SubmitChanges();
            }
        }
    }
}
